<?php

namespace catchAdmin\basics\controller;

use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\basics\model\Company as CompanyModel;
use catcher\CatchAuth;

class Company extends CatchController
{
    
    protected $companyModel;
    
    /**
     *
     * @time 2022/03/02 15:39
     * @param CompanyModel $companyModel
     * @return mixed
     */
    public function __construct(CompanyModel $companyModel)
    {
        $this->companyModel = $companyModel;
    }
    
    /**
     *
     * @time 2022/03/02 15:39
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function index(CatchAuth $auth)
    {
        $user = $auth->user();
        return CatchResponse::paginate($this->companyModel->getList($user['tenant_id']));
    }

    /**
     *
     * @time 2022/02/28 14:25
     * @param CreateRequest $request
     * @return \think\response\Json
     */
    public function create(CatchAuth $auth,Request $request)
    {
        $params = $request->filterEmptyField()->post();
        $user = $auth->user();
        $params['tenant_id'] = $user['tenant_id'];
        return CatchResponse::success($this->companyModel->storeBy($params),"新建成功");
    }
    
    /**
     *
     * @time 2022/03/02 15:39
     * @param CreateRequest $request
     * @return \think\response\Json
     */
    public function update(Request $request)
    {
        $params = $request->filterEmptyField()->post();
        return CatchResponse::success($this->companyModel->updateBy($params['id'], $params),'修改成功');
    }
    
    /**
     *
     * @time 2022/03/02 15:39
     * @param CreateRequest $request
     * @return \think\response\Json
     */
    public function delete(Request $request)
    {
        $id = $request->post('id');
        return CatchResponse::success($this->companyModel->deleteBy($id));
    }
}